<?php
/**
 * 产品报价管理
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class ProductPriceController extends BaseController {

	public function product_price(){
		//报价列表	
		$arr = $_GET;
		$PPrice = D("ProductPrice");
		$product_price = $PPrice->index($arr);
		e_json($product_price);	
	}
	public function product_price_add(){
		//报价产品添加

		$PPrice = D("ProductPrice");
		$arr = $PPrice->product_price_add($_POST);
		if($arr){
			$res['rcode'] = 1;
		}else{
			$res['rcode'] ="添加失败";
		}
		e_json($res);
	}
	public function notStandarder(){
		
		$user  = D("ProductPrice")->User();

		$Pprice = M("product_price");
		$v["price_id"]= I("get.price_id");
		if($v["price_id"]){
			$res['save_price'] = $Pprice 
							->field("product.product_id,product.name as product_name,product.sub_company,product_price.*")
							->join("product ON product_price.product_id = product.product_id")
							->where($v)
							->find();
			if($user['user_id'] == "1"){
				$wheres['sector_id'] = 	$res['save_price']['sub_company'];
				$company = M("sector")->field("sector_name")->where($wheres)->find();
				$res['save_price']['product_name'] = $company['sector_name']." ".$res['save_price']['product_name'];
			}
			$res['save_price']['free_price'] = $res['save_price']['free_price'] + $res['save_price']['service_price'];
		}

		$Sector = M("sector");
		$Pproduct = M("product");
		if($user['user_id'] == "1"){
			$res['product'] = $Pproduct->field("product_id,name,sub_company")->select();
			foreach ($res['product'] as &$value) {
				if($value['sub_company'] > 0){
					$map['sector_id'] = $value['sub_company'];
					$pcompany = $Sector->field("sector_name")->where($map)->find();
					$value['company_name'] = $pcompany['sector_name'];
				}else{
					$value['company_name'] = "总公司";
				}
			}
		}else{
			$maps['sector_id'] = $user['sector_id'];
			$user_company = $Sector->field("parent_company")->where($maps)->find();
			$where['sub_company'] = $user_company['parent_company'];
			if($this->m_is_admin == false){
				$where['product_type'] = 1;
			}
			$res['product'] = $Pproduct->field("product_id,name")->where($where)->select();	
		}

		//合同期限
		$res['service_time'] = array();
		$res['service_time']['0'] = array("id"=>"12m","time"=>"按年"); 
		$res['service_time']['1'] = array("id"=>"6m","time"=>"按半年");
		$res['service_time']['2'] = array("id"=>"3m","time"=>"按季度");
		$res['service_time']['3'] = array("id"=>"1m","time"=>"按月度");
		e_json($res);
	}

	public function save_product_price(){
		//修改报价

        $res['arr'] = $_POST;
        $PPrice = D("ProductPrice");
		$arr = $PPrice->save_product_price($res['arr']);
        e_json($arr);
	}
	//删除
	public function Product_price_delete(){
		$map['price_id'] = I("price_id");
		$Contract = M("contract");
		$count = $Contract->where($map)->find();
		if(!$count) {
			M("product_price")->where($map)->delete();
			$re = array('rcode'=>1,'data'=>0 ,'msg'=>'ok' );
		}else{
			$re = array('rcode'=>1,'data'=>1 ,'msg'=>'ok' );
		}
		e_json($re);	
	}
	//默认搜索数据
	public function echk(){

		//所属产品
		$PProduct = M("product");
    	$user  = D("ProductPrice")->User();
    	$maps['sector_id'] = $user['sector_id']; 
		$company = M("sector")->field("parent_company")->where($maps)->find();
		if($user['user_id'] !="1" ){
			if($company['parent_company'] != "1"){
				$where['sub_company'] = $company['parent_company'];
			}else{
				$where['sub_company'] = $user['sector_id'];
			}
			$res['product'] = $PProduct->field("name,product_id,sub_company")->where($where)->order("product_id desc")->select(); 
		}else{
			$res['product'] = $PProduct->field("name,product_id,sub_company")->order("product_id desc")->select();
			foreach ($res['product'] as &$value) {
			 	if($value['sub_company'] > 1){
			 		$map['sector_id'] = $value['sub_company'];
			 		$company = M("sector")->field("sector_name")->where($map)->find();
			 		$value['company_name'] = $company['sector_name'];
			 	}else{
			 		$value['company_name'] = "总公司";
			 	}
			 } 
		}

        $vb = count($res['product'])+1;
    	$res['product'][$vb]['name'] = "全部产品";
    	$res['product'] = array_reverse($res['product']);

        //分公司
        $wmap['parent_company'] = '1';
        $res['company'] = M("sector")->field("sector_name,sector_id")->where($wmap)->select();
        foreach ($res['company'] as &$value) {
        	$value['sector_name'] = mb_substr($value['sector_name'],0,5,'utf-8');
        }
        $v = count($res['company'])+1;
        $res['company'][$v]['sector_name'] = "总公司";
        $res['company'][$v]['sector_id'] = "";
       	$res['company'] = array_reverse($res['company']);

       	$res['user']['user_id'] = $user['user_id'];
		e_json($res);
	}
}
